/**
 * @file drv_misc.h
 * @brief 智慧电源私有杂项驱动
 * @author wangh (wanghuan3037@fiberhome.com)
 * @version 1.0
 * @date 2020-10-28
 * @copyright Copyright (c) 2020  烽火通信
 */
#ifndef __DRV_MISC_H
#define __DRV_MISC_H 
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "bsp_adc.h"
#include "bsp_dac.h"
#include "bsp_gpio.h"

#define PCU_24V_SET             ( 1 << 0 )  ///< 24V输出
#define PCU_12VOUT1_SET         ( 1 << 1 )  ///< 12V输出1
#define PCU_12VOUT2_SET         ( 1 << 2 )  ///< 12V输出2
#define PCU_12VOUT3_SET         ( 1 << 3 )  ///< 12V输出3
#define PCU_485_VCC1_SET        ( 1 << 4 )  ///< 485电源输出1
#define PCU_485_VCC2_SET        ( 1 << 5 )  ///< 485电源输出2
#define PCU_AC_Ctr_SET          ( 1 << 6 )  ///< 交流总开关
#define PCU_PWR_ALL_SET         ((uint8_t)0x7F) ///< 电源输出全开

/** @brief 智慧电源检测（ADC） */
typedef struct pcuAdc_s
{
    Adc_t ADC_TC_obj;
    Adc_t ADC_12V_obj;
    Adc_t ADC_24V_obj;
    Adc_t Isense_12V_1_obj;
    Adc_t Isense_12V_2_obj;
    Adc_t Isense_12V_3_obj;
    Adc_t Isense_24V_obj;
    Adc_t Isense_485_VCC1_obj;
    Adc_t Isense_485_VCC2_obj;

    float V_TC;
    float V_12V;
    float V_24V;
    float I_12V_1;
    float I_12V_2;
    float I_12V_3;
    float I_24V;
    float I_485_VCC1;
    float I_485_VCC2;

    bool IsInitialized;
    SemaphoreHandle_t Lock;
} pcuAdc_t;

/** @brief 智慧电源调光（DAC） */
typedef struct pcuDac_s
{
    Dac_t dac1_obj;
    Dac_t dac2_obj;

    float vol1;
    float vol2;

    bool IsInitialized;
    SemaphoreHandle_t Lock;
} pcuDac_t;

/** @brief 智慧电源检测（DI） */
typedef struct pwrDect_s
{
    Gpio_t BAT_CTRL_obj;
    Gpio_t Power_DECT1_obj;
    Gpio_t Power_DECT2_obj;
    Gpio_t Power_DECT3_obj;
    Gpio_t Power_DECT4_obj;
    Gpio_t Power_DECT5_obj;

    uint8_t v_BAT_CTRL;
    uint8_t v_Power_DECT1;
    uint8_t v_Power_DECT2;
    uint8_t v_Power_DECT3;
    uint8_t v_Power_DECT4;
    uint8_t v_Power_DECT5;

    bool IsInitialized;
    SemaphoreHandle_t Lock;
} pwrDect_t;


/**
 * @brief 智慧电源ADC检测初始化
 * @param  obj         电源ADC检测对象
 */
int8_t drv_pcuAdc_init( pcuAdc_t *obj );

/**
 * @brief 智慧电源ADC检测读取
 * @param  obj         电源ADC检测对象
 */
int8_t drv_pcuAdc_Get( pcuAdc_t *obj );

/**
 * @brief 智慧电源DAC调光初始化
 * @param  obj         DAC调光对象
 */
int8_t drv_pcuDac_init( pcuDac_t *obj );

/**
 * @brief 智慧电源DAC调光设置
 * @param  obj         DAC调光对象
 * @param  value1      通道1调光电压（0-10）
 * @param  value2      通道2调光电压（0-10）
 */
int8_t drv_pcuDac_Set( pcuDac_t *obj, float value1, float value2 );

/**
 * @brief 智慧电源DI检测读取
 * @param  obj         电源DI检测对象
 */
int8_t drv_pwrDect_init( pwrDect_t *obj );

/**
 * @brief 智慧电源DI状态读取
 * @param  obj         电源DI检测对象
 */
int8_t drv_pwrDect_Get( pwrDect_t *obj );

/**
 * @brief 智慧电源电源输出控制初始化
 */
void drv_pcuPwrCtr_init( void );

/**
 * @brief 电源输出控制
 * @param  mask        PCU_XX_SET 的组合
 * @param  state       0:置低，非0：置高
 * @par 示例:
 * @code
 *    drv_pwrCtr_Set( PCU_24V_SET | PCU_12VOUT1_SET | PCU_485_VCC2_SET );
 * @endcode
 */
void drv_pcuPwrCtr_Set( uint8_t mask, uint8_t state );

/**
 * @brief 获取电源输出当前状态
 */
void drv_pcuPwrCtr_Get( uint8_t *mask );

#endif	 // __DRV_MISC_H 

/*********** (C) COPYRIGHT 2020 FiberHome *****END OF FILE****/
